﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using famails.common;
using famails.dto;

namespace famails.dao
{
    /// <summary>
    /// Summary description for CategoryDTO
    /// </summary>
    public class CategoryDAO
    {
        public CategoryDAO()
        {

        }
        public DataTable GetByID(int ID)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblBlog_Categories WHERE CategoryID = @ID", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetAll(string name, int show)
        {
            SqlCommand cmd = new SqlCommand("SELECT a.*, case IsShow when 1 then N'Hiển thị' else N'Ẩn' end Show FROM tblBlog_Categories a WHERE isdelete <> 1 and (@show = -1 or isShow=@show) and categoryname like N'%"+@name+"%'", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@show", SqlDbType.Int).Value = show;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public bool Insert(CategoryDTO dto)
        {
            SqlCommand cmd = new SqlCommand("insert into tblBlog_Categories(CategoryName, IsShow, Description, CreateDate)values(@name, @show, @description, getdate())", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@show", SqlDbType.Int).Value = dto.IsShow;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.CategoryName;
            cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = dto.Description;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Edit(CategoryDTO dto)
        {
            SqlCommand cmd = new SqlCommand("update tblBlog_Categories set CategoryName=@name, IsShow=@show, Description=@Description where categoryid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@show", SqlDbType.Int).Value = dto.IsShow;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dto.CategoryName;
            cmd.Parameters.Add("@description", SqlDbType.NVarChar).Value = dto.Description;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = dto.CategoryID;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        public bool Delete(int id)
        {
            SqlCommand cmd = new SqlCommand("update tblBlog_Categories set isdelete=1 where categoryid = @id", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            int i = cmd.ExecuteNonQuery();
            cmd.Dispose();
            return i > 0;
        }

        internal bool CheckExistsName(string name, int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblBlog_Categories WHERE categoryid <> @id and categoryname = @name and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = name;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count > 0;
        }

        internal bool CheckBeforeDelete(int id)
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM tblBlog_Groups WHERE categoryid = @id and isdelete=0", ConnectionData._MyConnection);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table.Rows.Count == 0;
        }
    }
}
